package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inStorage.AllocatingInStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageAllocatingGetListRequestBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageAllocatingListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageAllocatingListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：wsy on 2017/12/1 16:55
 * <p>
 * 邮箱：445979770@qq.com
 */

public class InStorageAllocatingListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView allocatingInStorageList;

    private List<InStorageAllocatingListBean.ReceiveInAllocatingStorageListBean> allData;

    private AllocatingInStorageListAdapter adapter;

    private String mInStorageNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_allocating_list);
        initView(InStorageAllocatingListActivity.this);
        initData(InStorageAllocatingListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        allocatingInStorageList = (PullableListView) activity.findViewById(R.id.allocatingInStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.allocatingInStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_in_storage_manage_allocating_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_in_storage_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        allocatingInStorageList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                initData(activity);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        resetData(activity);
//        setmScanListener((String scanResult, int length) -> {
//            getData(activity, QRCodeUtils.DecryptQRCode(scanResult));
//        }, QRCodeConstant.TYPE_PURCHASE_ORDER);
//        getData(activity, mInStorageNumber);
    }
    private void resetData(Context context) {
        mInStorageNumber = "";
        allData = new ArrayList<>();
        addInStorageAllocatingListData(context, allData, allocatingInStorageList, true);
    }
    private void addInStorageAllocatingListData(Context context, List<InStorageAllocatingListBean.ReceiveInAllocatingStorageListBean> receiveInStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new AllocatingInStorageListAdapter(context, receiveInStorageList, mInStorageNumber, (String inStorageNumber) -> {
                mInStorageNumber = inStorageNumber;
                getData(context, mInStorageNumber);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String condition) {
        InStorageAllocatingGetListRequestBean bean = InStorageUtils.getAllocatingGetListRequestBean(condition);
        InStorageAllocatingListRequest.request(context, bean, new GsonBeanListener<InStorageAllocatingListBean>() {
            @Override
            public void onSuccess(InStorageAllocatingListBean bean) {
                allData = bean.getBody();
                if (allData==null || allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                mInStorageNumber = condition;
                addInStorageAllocatingListData(context, ActivityUtils.getShowData(allData, 0), allocatingInStorageList, true);
            }
        });
    }

    public interface InStorageAllocatingListener {
        void onNoDoubleClickListener(String inStorageNumber);
    }
}
